package com.mad.game.view.models;

import java.awt.Dimension;
import java.awt.Point;

import com.mad.game.backend.GameDemo;
import com.mad.game.backend.entities.GameEntity;
import com.mad.game.system.Vector2;

public class ModelEntity extends GameModel {
	
	public Vector2 MOVE_VEL = new Vector2(5, 0); 
	public Vector2 JUMP_VEL = new Vector2(0, 35);
	
	private Vector2 currentJumpVelocity = Vector2.NULL_VECTOR; 
	
	private boolean jumping ; 
	
	private boolean movingLeft = false, 
					movingRight = false ; 

	private GameEntity entity ; 
	
	public boolean isJumping() {
		return jumping;
	}

	public void setJumping(boolean jumping) {
		this.jumping = jumping;
	}

	public boolean isMovingLeft() {
		return movingLeft;
	}

	public void setMovingLeft(boolean movingLeft) {
		this.movingLeft = movingLeft;
	}

	public boolean isMovingRight() {
		return movingRight;
	}

	public void setMovingRight(boolean movingRight) {
		this.movingRight = movingRight;
	}

	public Vector2 getCurrentJumpVelocity() {
		return currentJumpVelocity;
	}

	public void setCurrentJumpVelocity(Vector2 currentJumpVelocity) {
		this.currentJumpVelocity = currentJumpVelocity;
	}

	public GameEntity getEntity() {
		return entity;
	}

	public void setEntity(GameEntity entity) {
		this.entity = entity;
	}

	public ModelEntity(final GameEntity entity, Point position, Dimension size) {
		super(position, size);
		this.setEntity(entity);
		this.MOVE_VEL = new Vector2(this.getEntity().getSpeed(), 0); 
		this.JUMP_VEL = new Vector2(0, this.getEntity().getJumpHeight());
	}
	
	private void toggleMoveLeft(boolean toggled){
		if(this.isMovingRight()) this.toggleMoveRight(false);
		this.setMovingLeft(toggled);
	}
	private void toggleMoveRight(boolean toggled){
		if(this.isMovingLeft()) this.toggleMoveLeft(false); 
		this.setMovingRight(toggled);
	}

	public void moveLeft(){
//		this.move(MOVE_VEL.invert()); 
		this.toggleMoveLeft(true); 
		this.jumpInMove(); 
	}
	
	public void moveRight(){
//		this.move(MOVE_VEL);
		this.toggleMoveRight(true);
		this.jumpInMove(); 
	}
	
	public void jumpInMove(){
		this.setCurrentJumpVelocity(JUMP_VEL.createCopy());
		this.setJumping(true);
	}
	
	public void jumpStraightUp(){
		this.jumpInMove();
		this.toggleMoveLeft(false);
		this.toggleMoveRight(false);
	}
	
	@Override
	public void update() {
		if(this.isJumping()){ 
			this.move(currentJumpVelocity);
			this.currentJumpVelocity.changeYByDelta(-(int)GRAVITY);
			if (this.isMovingRight())
				this.move(MOVE_VEL); 
			else if (this.isMovingLeft())
				this.move(MOVE_VEL.invert());
		}
		if(this.getCurrentJumpVelocity().isLessThanOrEqual(Vector2.Axis.Y, 0) && 
				GameDemo.getGameInstance().doesModelCollide_inMovement(this))
			this.setJumping(false); 
		super.update();
	}
	
	public void shoot(){
		GameDemo.getGameInstance().pauseLevel(); 
		int spawnX = (int) 
			(this.isMovingLeft() ? - ModelBullet.SIZE - this.MOVE_VEL.getX() : 
				this.getSize().getWidth()+ModelBullet.SIZE + this.MOVE_VEL.getX());
		int vx = this.isMovingLeft() ? -ModelBullet.VEL_X : ModelBullet.VEL_X ; 
		int x = (int) (this.getPosition().getX() + spawnX);
		int y = (int) (this.getPosition().getY() + this.getSize().getHeight()/2); 
		Point pos = new Point(x, y);
		ModelBullet newBullet = new ModelBullet(pos);
		newBullet.setVelocity(new Vector2(vx, ModelBullet.VEL_Y));
		GameDemo.getGameInstance().addNewModel(String.valueOf(newBullet.hashCode()), newBullet);
		GameDemo.getGameInstance().continueLevel(); 
	}

	public int getHealth() {
		return entity.getHealth();
	}

	public void setHealth(int health) {
		entity.setHealth(health);
	}

	public boolean isDead() {
		return entity.isDead();
	}

	public void setDead(boolean isDead) {
		entity.setDead(isDead);
	}

	public void changeHealth(int delta) {
		entity.changeHealth(delta);
	}

	public void die() {
		entity.die();
	}
	
	
	
}
